package com.imodia.checkoo.webtag;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hsq.commons.util.BeanFactory;
import com.imodia.checkoo.bean.CmsPageAdBean;
import com.imodia.checkoo.service.CmsPageAdService;

public class AdListTag  extends RequestContextAwareTag  {
	private static final long serialVersionUID = -1764906844122921796L;
	private TldParam tldParam;
	
	public TldParam getTldParam() {
		return tldParam;
	}

	public void setTldParam(TldParam tldParam) {
		this.tldParam = tldParam;
	}

	@Override
	protected int doStartTagInternal() {
		CmsPageAdService cmsPageAdService = (CmsPageAdService) BeanFactory.getBean("cmsPageAdService");
		List<CmsPageAdBean> pageAdList = cmsPageAdService.queryByCatalogueId(tldParam.getCataId(),tldParam.getEndNum());
		pageContext.setAttribute("AdListTag", pageAdList);

		if (tldParam.getHtmls() != null && tldParam.getHtmls().trim().length() > 0) {
			Writer out = pageContext.getOut();
			StringBuffer buf = new StringBuffer();
			try {
				buf.append(TldUtil.convertAd(pageAdList, tldParam));
				out.write(buf.toString());
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			out = null;
		}
		return EVAL_BODY_INCLUDE;
	}

}
